package com.reguang.controller.v1;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.reguang.annotation.ParamsNotNull;
import com.reguang.annotation.RequestLimit;
import com.reguang.config.TokenConfig;
import com.reguang.result.Result;
import com.reguang.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(value = "用户令牌", tags = { "用户令牌" })
@RequestMapping("api/v1/token")
public class TokenController {

	@Autowired
	private UserService userService;

	@Autowired
	private TokenConfig tokenConfig;

	/**
	 * 
	 * <p>Title: getToken</p>
	 * <p>Description: 获取令牌</p>
	 * @param code
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/issue")
	@RequestLimit(count = 10, time = 60000)
	@ApiOperation(value = "用户获取令牌", notes = "用户获取令牌接口")
	public Result getToken(@RequestParam(name = "code", required = true) @ParamsNotNull String code,
			HttpServletRequest request) throws Exception {
		return Result.success(userService.getToken(code));
	}

	/**
	 * 
	 * <p>Title: verifyToken</p>
	 * <p>Description: 校验令牌是否有效</p>
	 * @param token
	 * @param request
	 * @return
	 */
	@PostMapping("/verify")
	@RequestLimit(count = 10, time = 60000)
	@ApiOperation(value = "校验令牌是否有效")
	public Result verifyToken(@RequestParam(name = "token", required = true) @ParamsNotNull String token,
			HttpServletRequest request) {
		Boolean result = tokenConfig.verifyToken(token);
		return Result.success(result);
	}
}
